library jm_flutter;

import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jm_flutter/src/config.dart';
import 'package:jm_flutter/src/ui/resources/resources.dart';
import 'package:jm_flutter/src/ui/routers/jm_page.dart';
import 'package:jm_flutter/src/ui/routers/routes.dart';
import 'package:jm_flutter/src/ui/ui.dart';
import 'package:path_provider/path_provider.dart';
import 'src/common/common.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

export 'src/common/common.dart';
export 'src/ui/ui.dart';
export 'src/config.dart';

export 'package:get/get.dart';

class JmApp extends StatelessWidget {
  JmApp({
    Key? key,
    this.home,
    this.initPage,
    this.debugShowCheckedModeBanner = false,
    this.pages,
    this.onInit,
  }) : super(key: key) {
    init();
  }

  final tag = "JmMaterialApp";
  final Widget? home;
  final String? initPage;
  final bool debugShowCheckedModeBanner;
  final List<JmPage>? pages;
  final VoidCallback? onInit;

  void init() async {
    logd(tag, "开始初始化");

    I18n.init();
    if (pages != null && pages!.isNotEmpty) {
      for (var page in pages!) {
        JmPages.pages.add(GetPage(name: page.name, page: () => page.page));
        logd(tag, "添加页面：" + page.name + " ," + JmPages.pages.length.toString());
      }
    } else {
      logd(tag, "未添加页面");
    }

    //AppVersion.init();
    initUi(colors: JmConfig.colors, assets: JmConfig.assets);
    Show.init();
  }

  @override
  Widget build(BuildContext context) {
    //init();
    //AppVersion.init();
    return AnimatedFluentTheme(
      //data: ThemeData(),
      data: FluentThemeData(
        accentColor: Colors.blue,
        visualDensity: VisualDensity.standard,
        fontFamily: 'NotoSansSC',
        focusTheme: FocusThemeData(
          glowFactor: is10footScreen(context) ? 2.0 : 0.0,
        ),
      ),
      child: GetMaterialApp(
        onInit: () {
          logd("Get", "onInit");
          AppVersion.init();
          onInit?.call();
        },
        locale: const Locale('en'),
        supportedLocales: const [
          Locale('en', ''), // English, no country code
        ],
        home: home,
        title: JmConfig.appName,
        enableLog: false,
        initialRoute: initPage,
        builder: EasyLoading.init(
            builder: I18n.initBuilder(FlutterI18n.rootAppBuilder())),
        localizationsDelegates: [
          //DefaultFluentLocalizations.delegate,
          I18n.flutterI18nDelegate,
          FluentLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        logWriterCallback: (String text, {bool? isError}) {
          if (text == "OPEN 低电弹窗") {
            //Show.toast("低电弹窗");
            //findApp.isShowLowPowerDialog = true;
          } else if (text == "CLOSE 低电弹窗") {
            //Show.toast("低电弹窗关闭");
            //findApp.isShowLowPowerDialog = false;
          }
          String pageName = "";
          if (text.contains("GOING TO ROUTE")) {
            pageName = text.substring(15);
            logd("Get", "进入页面:" + pageName);
            // if(pageName == Routes.startPage){
            //   logd("logWriterCallback", "忽略当前页统计");
            //   return;
            // }
            //Analytics.pageStart(text.substring(15));
          } else if (text.contains("CLOSE TO ROUTE")) {
            pageName = text.substring(15);
            logd("Get", "退出页面:" + pageName);
            //Analytics.pageEnd(text.substring(15));
          } else {
            logd("Get", text);
          }
        },
        debugShowCheckedModeBanner: debugShowCheckedModeBanner,
        getPages: JmPages.pages,
      ),
    );
  }
}

class JmFlutter {
  static style({JmColorsData? colors}) {}

  static Future<void> init() async {
    //await GetStorage.init();
    await Storage.init(title: JmConfig.appName);
  }
}
